<?php
/**
 * Created by PhpStorm.
 * User: spatra
 * Date: 15-1-7
 * Time: 下午8:08
 */

use \Symfony\Component\HttpFoundation\File\UploadedFile as UploadFile;

class TempFileController extends \BaseController
{
    public function postImage()
    {
        $validator = $this->getImageValidator(Input::all());

        if( $validator->passes() ){
            $file = Input::file('file');

            $tmpFilename =  $this->getFilename($file);
            $file->move($this->getTmpDirectory(), $tmpFilename);

            return Response::json([
               'filename'=>$tmpFilename
            ]);

        }else{
            return Response::json([
               'error' => $this->changeValidatorMessageToString( $validator->getMessageBag() )
            ]);
        }
    }

    protected function getImageValidator($postData)
    {
        $rules = [
            'file'=>'mimes:jpg,jpeg,png|max:1024'
        ];

        return Validator::make($postData, $rules);
    }

    protected function getFilename(UploadFile $file)
    {
        return Auth::user()['id'] . '-temp.'. $file->getClientOriginalExtension();
    }

    protected function getTmpDirectory()
    {
        return public_path(). '/temp/';
    }
}